package com.newtouch.avmarketing.mgr.modules.avmarkrting.util;

import cn.hutool.core.date.DateTime;
import com.newtouch.avmarketing.mgr.modules.avmarkrting.entity.DoubleAuditing;
import com.newtouch.avmarketing.mgr.modules.avmarkrting.model.Result;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

public class ExportUtil {

    /**
     * 双录审核信息导出Excel
     */
    public static void getExport(HttpServletResponse response,List<DoubleAuditing> list) throws IOException {
        String[] tableHeaders =
                {"序号","分公司", "中支机构", "四级机构", "产品名称", "投保单号",
                "是否生效", "投保人", "营销员工号", "联系方式", "投保日期", "审核状态"};

        String filePath = new DateTime().toString("yyyyMMdd");
        String filename = "double";
        String newfilename = filePath+"/"+filename;
        try {
            SXSSFWorkbook book = new SXSSFWorkbook();
            SXSSFSheet sheet = book.createSheet(filename);

            //设置单元格风格，居中对齐.
            CellStyle cs = book.createCellStyle();
            //设置字体:
            Font font = book.createFont();
            font.setFontName("黑体");
            font.setFontHeightInPoints((short) 12);//设置字体大小
            cs.setFont(font);//要用到的字体格式

            //自动设置列宽
            autoSizeColumns(sheet, tableHeaders.length + 1);

            Row row = sheet.createRow(0);

            // 创建表头
            for (int i = 0; i < tableHeaders.length; i++) {
                Cell cell = row.createCell(i);
                cell.setCellValue(tableHeaders[i]);
                cell.setCellStyle(cs);
            }
            for (int i = 0; i < list.size(); i++) {
                row = sheet.createRow(i + 1);
                DoubleAuditing doubleAuditing = list.get(i);

                row.createCell(0).setCellValue(i + 1);
                row.createCell(1).setCellValue(doubleAuditing.getUnitName());
                row.createCell(2).setCellValue(doubleAuditing.getBranchName());
                row.createCell(3).setCellValue(doubleAuditing.getFourBranchName());
                row.createCell(4).setCellValue(doubleAuditing.getProductName());
                row.createCell(5).setCellValue(doubleAuditing.getPolicyCode());
                switch (doubleAuditing.getIsValid()) {
                    case "1":
                        row.createCell(6).setCellValue("是");
                        break;
                    case "0":
                        row.createCell(6).setCellValue("否");
                        break;
                }
                row.createCell(7).setCellValue(doubleAuditing.getPolicyName());
                row.createCell(8).setCellValue(doubleAuditing.getMarketEmp());
                row.createCell(9).setCellValue(doubleAuditing.getPhoneNumber());
                row.createCell(10).setCellValue(dateFormatter(doubleAuditing.getInsureTime()));
                switch (doubleAuditing.getCheckStatus()) {
                    case "0":
                        row.createCell(11).setCellValue("待审核");
                        break;
                    case "1":
                        row.createCell(11).setCellValue("审核通过");
                        break;
                    case "2":
                        row.createCell(11).setCellValue("审核不通过");
                        break;
                }
            }
            response.reset();

            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"); // 设置下载类型
            response.setHeader("Content-disposition", "attachment;filename=" + newfilename+ ".xlsx");
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setHeader("Cache-Control","no-cache");
            response.setCharacterEncoding("utf-8");

            ServletOutputStream sos = response.getOutputStream();
            book.write(sos);
            sos.flush();
            sos.close();
        } catch (Exception e) {
            System.out.println("文件输出失败!");
            e.printStackTrace();
        }
    }

    public static String dateFormatter(Object obj) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (obj == null) {
            return null;
        } else {
            Date date = null;
            if (obj instanceof String) {  //属于string
                try {
                    date = sdf.parse(obj.toString());
                    String format = sdf.format(date);
                    return format;
                } catch (Exception e) {
                    return null;
                }
            } else if (obj instanceof Date) { // 属于date
                date = (Date) obj;
                String format = sdf.format(date);
                return format;
            }
            return null;
        }
    }
    /**
     * 自动调整列宽
     * @param sheet
     * @param columnNumber
     */
    private static void autoSizeColumns(SXSSFSheet sheet, int columnNumber) {
        // 自动调整列宽
        sheet.trackAllColumnsForAutoSizing();
        for (int i = 0; i < columnNumber; i++) {
            sheet.autoSizeColumn(i,true);
        }
    }
}
